package org.dromara.third.convert.troubleShootingTask.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.TroubleShootingTask;
import org.dromara.system.mapper.TroubleShootingTaskMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.troubleShootingTask.dto.TroubleShootingTaksDTO;
import org.dromara.third.convert.troubleShootingTask.validator.TroubleShootingTaskValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertTroubleShootingTaskController {

    @Autowired
    private TroubleShootingTaskMapper troubleShootingTaskMapper;

    @PostMapping("/troubleShootingTask")
    @Transactional
    public R<Void> addTroubleShootingTask(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        TroubleShootingTaksDTO troubleShootingTaksDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            troubleShootingTaksDTO = JSONObject.parseObject(decrypt, TroubleShootingTaksDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, troubleShootingTaksDTO, new TroubleShootingTaskValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        TroubleShootingTask troubleShootingTask = assembleTroubleShootingTaskData(orgCode, troubleShootingTaksDTO);
        int count = troubleShootingTaskMapper.countWithOutDelFlag(troubleShootingTask.getId());
        if (count == 0) {
            troubleShootingTaskMapper.insert(troubleShootingTask);
        } else {
            troubleShootingTaskMapper.updateWithOutDelFlag(troubleShootingTask);
        }
        return R.ok();
    }

    private TroubleShootingTask assembleTroubleShootingTaskData(String orgCode, TroubleShootingTaksDTO dto) {
        TroubleShootingTask troubleShootingTask = new TroubleShootingTask();
        try {
            troubleShootingTask.setId(dto.getId());
            troubleShootingTask.setRiskMeasureId(dto.getRiskMeasureId());
            troubleShootingTask.setCheckUserName(dto.getCheckUserName());
            troubleShootingTask.setCheckCycle(dto.getCheckCycle());
            troubleShootingTask.setCheckCycleUnit(dto.getCheckCycleUnit());
            troubleShootingTask.setTroubleShootContent(dto.getTroubleShootContent());
            troubleShootingTask.setWorkType(dto.getWorkType());
            troubleShootingTask.setTaskNum(dto.getTaskNum());
            troubleShootingTask.setWorkDayType(dto.getWorkDayType());
            troubleShootingTask.setWorkStartTime(dto.getWorkStartTime());
            troubleShootingTask.setWorkEndTime(dto.getWorkEndTime());
            troubleShootingTask.setTaskStartTime(DateUtils.parseDate(dto.getTaskStartTime()));
            troubleShootingTask.setTaskEndTime(DateUtils.parseDate(dto.getTaskEndTime()));
            troubleShootingTask.setDelFlag(Long.parseLong(dto.getDelFlag()));
            troubleShootingTask.setCreateDept(Long.parseLong(dto.getCreateDept()));
            troubleShootingTask.setCreateBy(Long.parseLong(dto.getCreateBy()));
            troubleShootingTask.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            troubleShootingTask.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            troubleShootingTask.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            troubleShootingTask.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装隐患排查任务信息异常");
        }
        return troubleShootingTask;
    }
}
